package cn.lixiaodan.hqlbuilder.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import org.jdesktop.application.Application;

import cn.lixiaodan.hqlbuilder.model.ClassModel;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ConditionPane extends javax.swing.JPanel {
	private JButton jButton1;
	private JComboBox jComboBox5;
	private JComboBox jComboBox4;
	private JComboBox jComboBox3;
	private JComboBox jComboBox2;
	private JComboBox jComboBox1;
	private NewSingleFrameApplication app;
	private static final String TOSELECT = "请选择";

	public String getExpr() {
		StringBuffer expr = new StringBuffer();
		expr.append(this.jComboBox1.getSelectedItem()).append(".").append(this.jComboBox2.getSelectedItem()).append(" ").append(this.jComboBox3.getSelectedItem()).append(" ").append(this.jComboBox4.getSelectedItem()).append(".").append(this.jComboBox5.getSelectedItem()).append(" ");
		return expr.toString();
	}

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new ConditionPane(null));
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public ConditionPane() {
		super();
		initGUI();
	}

	public ConditionPane(NewSingleFrameApplication app) {
		super();
		this.app = app;
		initGUI();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(486, 25));
			{
				jButton1 = new JButton();
				this.add(jButton1);
				jButton1.setName("jButton1");
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jButton1ActionPerformed(evt);
					}
				});
			}
			{
				ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(new String[] { TOSELECT });
				jComboBox1 = new JComboBox();
				this.add(jComboBox1);
				jComboBox1.setModel(jComboBox1Model);
				jComboBox1.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent evt) {
						jComboBox1ItemStateChanged(evt);
					}
				});
				jComboBox1.addPopupMenuListener(new PopupMenuListener() {
					public void popupMenuWillBecomeVisible(PopupMenuEvent evt) {
						jComboBox1PopupMenuWillBecomeVisible(evt);
					}

					public void popupMenuWillBecomeInvisible(PopupMenuEvent evt) {
					}

					public void popupMenuCanceled(PopupMenuEvent evt) {
					}
				});
			}
			{
				ComboBoxModel jComboBox2Model = new DefaultComboBoxModel(new String[] { TOSELECT });
				jComboBox2 = new JComboBox();
				this.add(jComboBox2);
				jComboBox2.setModel(jComboBox2Model);
			}
			{
				ComboBoxModel jComboBox3Model = new DefaultComboBoxModel(new String[] { "=", "<>", "!=", ">", "<", ">=", "<=" });
				jComboBox3 = new JComboBox();
				this.add(jComboBox3);
				jComboBox3.setModel(jComboBox3Model);
			}
			{
				ComboBoxModel jComboBox4Model = new DefaultComboBoxModel(new String[] { TOSELECT });
				jComboBox4 = new JComboBox();
				this.add(jComboBox4);
				jComboBox4.setModel(jComboBox4Model);
				jComboBox4.addItemListener(new ItemListener() {
					@Override
					public void itemStateChanged(ItemEvent e) {
						jComboBox4ItemStateChanged(e);
					}
				});
				jComboBox4.addPopupMenuListener(new PopupMenuListener() {
					public void popupMenuWillBecomeVisible(PopupMenuEvent evt) {
						jComboBox4PopupMenuWillBecomeVisible(evt);
					}

					public void popupMenuWillBecomeInvisible(PopupMenuEvent evt) {
					}

					public void popupMenuCanceled(PopupMenuEvent evt) {
					}
				});
			}
			{
				ComboBoxModel jComboBox5Model = new DefaultComboBoxModel(new String[] { TOSELECT });
				jComboBox5 = new JComboBox();
				this.add(jComboBox5);
				jComboBox5.setModel(jComboBox5Model);
			}
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 下拉填充类
	 * 
	 * @param cb
	 */
	private void comboFillClass(JComboBox cb) {
		DefaultComboBoxModel model = (DefaultComboBoxModel) cb.getModel();
		while (model.getSize() > 1) {
			model.removeElementAt(1);
		}
		List<ClassInternalFrame> ciflist = this.app.ciflist;
		for (int i = 0; i != ciflist.size(); i++) {
			ClassModel cmodel = ciflist.get(i).getClassModel();
			model.addElement(cmodel);
		}
	}

	/**
	 * 下拉填充字段名
	 * 
	 * @param cb
	 */
	private void comboFillField(ClassModel cmodel, JComboBox cb) {
		List<String> cols = cmodel.getFields();

		DefaultComboBoxModel model = (DefaultComboBoxModel) cb.getModel();
		while (model.getSize() > 1) {
			model.removeElementAt(1);
		}
		List<ClassInternalFrame> ciflist = this.app.ciflist;
		for (int i = 0; i != cols.size(); i++) {
			model.addElement(cols.get(i));
		}
	}

	private void jButton1ActionPerformed(ActionEvent evt) {
		this.getParent().invalidate();
		this.getParent().remove(this);
		this.getParent().validate();
		this.app.cplist.remove(this);
	}

	private void jComboBox1PopupMenuWillBecomeVisible(PopupMenuEvent evt) {
		comboFillClass((JComboBox) evt.getSource());
	}

	private void jComboBox4PopupMenuWillBecomeVisible(PopupMenuEvent evt) {
		comboFillClass((JComboBox) evt.getSource());
	}

	private void jComboBox1ItemStateChanged(ItemEvent evt) {
		if (evt.getStateChange() != ItemEvent.SELECTED)
			return;
		JComboBox cbo = (JComboBox) evt.getSource();
		Object selected = cbo.getSelectedItem();
		if (selected instanceof ClassModel)
			comboFillField((ClassModel) selected, this.jComboBox2);

	}

	private void jComboBox4ItemStateChanged(ItemEvent evt) {
		if (evt.getStateChange() != ItemEvent.SELECTED)
			return;
		JComboBox cbo = (JComboBox) evt.getSource();
		Object selected = cbo.getSelectedItem();
		if (selected instanceof ClassModel)
			comboFillField((ClassModel) selected, this.jComboBox5);

	}
}
